﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Automobile.service.Model;
using Automobile.service.Common;
using System.Data;

namespace Automobile.service.DAL
{
    public class WareHoseDAL
    {
        SQLHelper h = new SQLHelper();
        /// <summary>
        /// 显示库房
        /// </summary>
        /// <returns></returns>
        public List<WareHose> ShowWareHose()
        {
            string sql = "select * from WareHose";
            List<WareHose> list = new List<WareHose>();
            DataTable data = h.ExecDataTable(sql);
            foreach (DataRow item in data.Rows)
            {
                WareHose w = new WareHose();
                w.WareHose_Id = Convert.ToInt32(item["WareHose_Id"]);
                w.WareHose_Name = item["WareHose_Name"].ToString();
                w.WareHose_People = item["WareHose_People"].ToString();
                w.WareHose_Phone = item["WareHose_Phone"].ToString();
                w.WareHose_Address = item["WareHose_Address"].ToString();
                w.WareHose_Remark = item["WareHose_Remark"].ToString();
                list.Add(w);
            }
            return list;
        }
        /// <summary>
        /// 添加库房
        /// </summary>
        /// <param name="w"></param>
        /// <returns></returns>
        public int AddWareHose(WareHose w)
        {
            string sql = "insert into WareHose(WareHose_Name,WareHose_People,WareHose_Phone,WareHose_Address,WareHose_Remark)values ('" + w.WareHose_Name + "','" + w.WareHose_People + "','" + w.WareHose_Phone + "','" + w.WareHose_Address + "','" + w.WareHose_Remark + "')";
            return h.ExecNonquery(sql);
        }
        /// <summary>
        /// 删除仓库
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int Del(int id)
        {
            string sql = "delete WareHose where WareHose_Id=" + id + "";
            return h.ExecNonquery(sql);
        }
        /// <summary>
        /// 根据id获取仓库单条信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public DataTable GetWareHoseById(int id)
        {
            string sql = "select * from WareHose where WareHose_Id=" + id + "";
            return h.ExecDataTable(sql);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="w"></param>
        /// <returns></returns>
        public int UpdWareHose(WareHose w)
        {
            string sql = "update WareHose set WareHose_Name='" + w.WareHose_Name + "',WareHose_People='" + w.WareHose_People + "',WareHose_Phone='" + w.WareHose_Phone + "',WareHose_Address='" + w.WareHose_Address + "',WareHose_Remark='" + w.WareHose_Remark + "' where WareHose_Id=" + w.WareHose_Id + "";
            return h.ExecNonquery(sql);
        }
        /// <summary>
        /// 根据仓库名查询库房
        /// </summary>
        /// <returns></returns>
        public List<WareHose> SearchWareHose(string WareHose_Name)
        {
            string sql = "select * from WareHose where WareHose_Name like '%" + WareHose_Name + "%'";
            List<WareHose> list = new List<WareHose>();
            DataTable data = h.ExecDataTable(sql);
            foreach (DataRow item in data.Rows)
            {
                WareHose w = new WareHose();
                w.WareHose_Id = Convert.ToInt32(item["WareHose_Id"]);
                w.WareHose_Name = item["WareHose_Name"].ToString();
                w.WareHose_People = item["WareHose_People"].ToString();
                w.WareHose_Phone = item["WareHose_Phone"].ToString();
                w.WareHose_Address = item["WareHose_Address"].ToString();
                w.WareHose_Remark = item["WareHose_Remark"].ToString();
                list.Add(w);
            }
            return list;
        }
        public int count()
        {
            string sql = "select count(*) from WareHose";
            return Convert.ToInt32(h.ExecScalar(sql));
        }
    }
}
